<?php

/**
 * This is the model class for table "{{help}}".
 *
 * The followings are the available columns in table '{{help}}':
 * @property integer $zhe_id
 * @property string $zhc_id
 * @property string $zhe_content
 * @property integer $zhe_highlight
 * @property integer $zhe_hist
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Help extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Help the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{help}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zhe_title,zhc_id,zhe_content', 'required'),
			array('zhe_highlight, zhe_hist, inputtime, updatetime,zhc_id', 'numerical', 'integerOnly'=>true),
			array('zhe_title', 'length', 'max'=>100, 'min'=>4),
			array('zhe_content', 'length', 'min'=>4),
			array('zhe_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zhe_id, zhc_id, zhe_content, zhe_highlight, zhe_hist, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'helpcat'=>array(self::BELONGS_TO, 'HelpCat', 'zhc_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zhe_id' => 'Zhe',
			'zhc_id' => '分类',
			'zhe_title' => '标题',
			'zhe_content' => '内容',
			'zhe_highlight' => 'Zhe Highlight',
			'zhe_hist' => 'Zhe Hist',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zhe_id',$this->zhe_id);
		$criteria->compare('zhe_title',$this->zhe_title,true);
		$criteria->compare('zhe_content',$this->zhe_content,true);
		$criteria->compare('zhe_highlight',$this->zhe_highlight);
		$criteria->compare('zhe_hist',$this->zhe_hist);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		//分类查询
		if(!empty($this->zhc_id)){
			$criteria->join = ' LEFT JOIN {{help_cat}} hc ON hc.zhc_id=t.zhc_id ';
			$criteria->addCondition(" (hc.zhc_id={$this->zhc_id} OR hc.zhc_parentid={$this->zhc_id} OR hc.zhc_topid={$this->zhc_id})");
		}

		//排序
		$criteria->order = "inputtime DESC";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 是否推荐
	 */
	public function getZheHighlight($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是'	
				);
		}else{
			$data = $this->getZheHighlight();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}
	
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}else{
			$this->updatetime = time();
		}
		return true;
	}
}